package com.lzc.springMvcPart;

import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static java.awt.AWTEventMulticaster.add;

@Configuration
@ComponentScan
public class WebConfig {

    /**
     * spring容器根据WebServiceFactory接口找到该对象,调用getService方法传入所有的ServletContextInitializer
     *
     * @return
     */
    @Bean
    public TomcatServletWebServerFactory tomcatServletWebServerFactory(){
        return new TomcatServletWebServerFactory();
    }

    /**
     * 绑定到容器后，在第一次访问该路径时实现初始化，调用servlet接口的init方法
     * 简介调用到refresh方法，实现里面的组件初始化
     * @return
     */
    @Bean
    public DispatcherServlet dispatcherServlet(){
        return new DispatcherServlet();
    }

    /**
     * spring容器启动时找到所有的ServletContextInitializer接口加入集合，传给WebServiceFactory
     *        启动时，调用所有的onStartUp方法传入web容器对象实现绑定
     * @param dispatcherServlet
     * @return
     */
    @Bean
    public DispatcherServletRegistrationBean dispatcherServletRegistrationBean(DispatcherServlet dispatcherServlet){
        return new DispatcherServletRegistrationBean(dispatcherServlet,"/");
    }

    /**
     * 在dispatcherServlet的initHandlerMappings方法中  容器中有HandlerMapping就不会走默认实现了
     * 作用初始化uri和控制器方法映射关系，，根据请求uri获取控制器方法
     * @return
     */
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping(){
        return new RequestMappingHandlerMapping();
    }

    /**
     * 覆盖默认实现,作用调用控制器方法
     * @return
     */
    @Bean
    public HandlerAdapterChild handlerAdapterChild(){
        HandlerAdapterChild handlerAdapterChild = new HandlerAdapterChild();

        List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
        argumentResolvers.add(new TokenArgumentResolver());

        List<HandlerMethodReturnValueHandler> returnValueHandlers = new ArrayList<>();
        returnValueHandlers.add(new JsonReturnResolver());

        handlerAdapterChild.setCustomArgumentResolvers(argumentResolvers);
        handlerAdapterChild.setCustomReturnValueHandlers(returnValueHandlers);
        return handlerAdapterChild;
    }

}
